package meteocal.gui.security;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityExistsException;
import meteocal.business.security.boundary.UserManager;
import meteocal.business.security.entity.User;

/**
 *
 * @author luana
 */

@Named
@RequestScoped
public class RegistrationBean {
    
    @EJB 
    private UserManager um;
    
    private User user;
    
    public RegistrationBean(){}

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
  
    public String register (){
        FacesContext context = FacesContext.getCurrentInstance();
        
        try{
            um.save(user);
            return "index?faces-redirect=true";
        }catch(Exception e){
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Registration Failed","This user already exists"));
            return "sign-up";
        }
        
    }
    
}
